const { Model, DataTypes } = require('sequelize');
const sequelize = require('../config/database');

class Payment extends Model {}

Payment.init({
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  order_id: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'orders',
      key: 'id'
    },
    comment: '订单ID'
  },
  amount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '支付金额（单位：分）'
  },
  method: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '支付方式：wechat/alipay'
  },
  payment_type: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '支付类型：JSAPI/H5/NATIVE等'
  },
  status: {
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: 'pending',
    comment: '支付状态：pending/processing/completed/failed/refunded'
  },
  app_id: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '支付应用ID'
  },
  prepay_id: {
    type: DataTypes.STRING,
    comment: '预支付交易会话标识'
  },
  transaction_id: {
    type: DataTypes.STRING,
    comment: '支付平台交易号'
  },
  paid_at: {
    type: DataTypes.DATE,
    comment: '支付完成时间'
  },
  response: {
    type: DataTypes.JSON,
    comment: '支付平台响应数据'
  },
  created_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '更新时间'
  }
}, {
  sequelize,
  modelName: 'Payment',
  tableName: 'payments',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      fields: ['order_id'],
      comment: '订单ID索引'
    },
    {
      fields: ['transaction_id'],
      comment: '支付平台交易号索引'
    },
    {
      fields: ['status'],
      comment: '支付状态索引'
    }
  ],
  comment: '支付信息表'
});

module.exports = Payment; 